/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.presenter.user;

import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.List;
import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import org.lucava.boatskeeper.client.PlaceMapper;
import org.lucava.boatskeeper.client.event.AddEvent;
import org.lucava.boatskeeper.client.event.EditEvent;
import org.lucava.boatskeeper.client.presenter.ViewPresenter;
import org.lucava.boatskeeper.shared.User;
import org.lucava.boatskeeper.shared.rpc.user.DeleteUsers;
import org.lucava.boatskeeper.shared.rpc.user.DeleteUsersResult;
import org.lucava.boatskeeper.shared.rpc.user.GetUsers;
import org.lucava.boatskeeper.shared.rpc.user.GetUsersResult;

/**
 *
 * @author glenn
 */
public class UsersPresenter extends ViewPresenter<UsersPresenter.Display> {

    public interface Display extends WidgetDisplay {

        HasClickHandlers getAddButton();

        HasClickHandlers getDeleteButton();

        Column getEditColumn();

        void setData(List<User> data);

        List<Integer> getSelectedRows();
    }
    private List<User> users;
    public static Place PLACE;

    public UsersPresenter(DispatchAsync dispatcher, EventBus eventBus, Display display) {
        super(dispatcher, eventBus, display);

        fetchUsers();
        bind();
    }

    @Override
    protected void onBind() {
        display.getAddButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                eventBus.fireEvent(new AddEvent(PlaceMapper.EDIT_USER));
            }
        });

        display.getDeleteButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                deleteSelectedUsers();
            }
        });

        display.getEditColumn().setFieldUpdater(new FieldUpdater<User, String>() {
            @Override
            public void update(int index, User object, String value) {
                Integer id = object.getId();
                eventBus.fireEvent(new EditEvent(PlaceMapper.EDIT_USER, id));
            }
        });


        /*display.getList().addClickHandler(new ClickHandler() {
         @Override
         public void onClick(ClickEvent event) {
         int selectedRow = display.getClickedRow(event);

         if (selectedRow >= 0) {
         Integer id = users.get(selectedRow).getId();
         eventBus.fireEvent(new EditEvent(PlaceMapper.EDIT_BOAT, id));
         }
         }
         });*/

    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public User getUser(int index) {
        return users.get(index);
    }

    private void fetchUsers() {
        getDispatcher().execute(new GetUsers(), new AsyncCallback<GetUsersResult>() {
            @Override
            public void onSuccess(GetUsersResult result) {
                users = result.getList();
                refreshDisplay();
            }

            @Override
            public void onFailure(Throwable caught) {
                System.out.println(caught.getMessage());
                Window.alert(caught.getMessage());
            }
        });
    }

    private void deleteSelectedUsers() {
        List<Integer> selectedRows = display.getSelectedRows();


        getDispatcher().execute(new DeleteUsers(selectedRows), new AsyncCallback<DeleteUsersResult>() {
            @Override
            public void onSuccess(DeleteUsersResult result) {
                users = result.getList();
                refreshDisplay();
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Error deleting selected contacts " + caught.getMessage());
            }
        });

    }

    @Override
    public void refreshDisplay() {

        display.setData(users);

    }

    @Override
    protected void onPlaceRequest(PlaceRequest request) {
        fetchUsers();
    }

    @Override
    public Place getPlace() {
        return PLACE;
    }
}